const state = {
  pageinfo: {},
  treeId: '',
  treeName: '',
  trees: [],
  currentTree: {},
  currentTab: '', // 待办/已办
  requestPending: false
}
import {
  isObject
} from '@admin/utils/index'
// import router from '@/router/index'

const mutations = {
  SET_PAGEINFO: (state, pageinfo) => {
    state.pageinfo = pageinfo
  },
  SET_TREEID: (state, treeId) => {
    state.treeId = treeId
  },
  SET_TREENAME: (state, treeName) => {
    state.treeName = treeName
  },
  SET_CURRENTTREE: (state, tree) => {
    window.$CD.treeInfo = tree
    window.$CD.treeId = tree[state.treeId] || ''
    state.currentTree = tree
  },
  SET_CURRENTTAB: (state, tab) => {
    state.currentTab = tab
  },
  SET_TREES: (state, trees) => {
    state.trees = trees
  },
  SET_PENDING: (state, pending) => {
    state.requestPending = pending
  }
}

const actions = {
  setPageInfo({
    commit
  }, pageinfo) {
    console.log(pageinfo)
    let treeId = ''
    let treeName = ''
    try {
      if (pageinfo && pageinfo.treeInfo) {
        const treeInfo = JSON.parse(pageinfo.treeInfo)
        treeId = treeInfo.keyField || ''
        treeName = (treeInfo.displayFields && treeInfo.displayFields.length) ? treeInfo.displayFields[0] : ''
      }
    } catch (e) {}
    commit('SET_TREEID', treeId)
    commit('SET_TREENAME', treeName)
    commit('SET_PAGEINFO', pageinfo)
    commit('SET_CURRENTTREE', {})
  }
}

const getters = {
  treeInfo: (state) => {
    try {
      return JSON.parse(state.pageinfo.treeInfo)
    } catch (e) {
      return {}
    }
  },
  tmp: (state) => {
    return state?.pageinfo?.tmp || ''
  },
  allTreeKeys: (state) => {
    return state.trees.filter(i => {
      return i[state.treeId] !== '0'
    }).map(i => {
      return i[state.treeId]
    })
  },
  allTreeNames: (state) => {
    return state.trees.filter(i => {
      return i[state.treeId] !== '0'
    }).map(i => {
      return i[state.treeName]
    })
  },
  clientMacro: (state, getters) => {
    let macro = {}
    // 选中树
    if (state.currentTree && Object.keys(state.currentTree).length && state.treeId) {
      if (getters.tmp === '1120' && state.currentTree[state.treeId] == '0') {
        // 标签表格，全部(0)查询时传其他tab的值
        macro['treeKey'] = getters.allTreeKeys.join(',')
        macro['treeName'] = getters.allTreeNames.join(',')
      } else {
        macro['treeKey'] = state.currentTree[state.treeId]
        macro['treeName'] = state.currentTree[state.treeName]
      }
    } else {
      macro['treeKey'] = ''
      macro['treeName'] = ''
    }
    // 从配置中拼接宏变量
    if (getters.treeInfo && getters.treeInfo.macroConfig && isObject(getters.treeInfo.macroConfig)) {
      const macroConfig = getters.treeInfo.macroConfig
      Object.keys(macroConfig).forEach(treeKey => {
        macro[treeKey] = state.currentTree[macroConfig[treeKey]]
      })
    }
    // pageinfo获取参数
    if (state.pageinfo.customParam) {
      try {
        macro = Object.assign(macro, JSON.parse(state.pageinfo.customParam))
      } catch (e) {}
    }
    // 路由参数parentDataId
    const router = window.qiBao.$router
    const query = router.app._route.query
    Object.keys((query || {})).forEach(key => {
      if (key === 'parentDataId' && query[key]) {
        macro['parentDataId'] = query[key]
      } else if (key === 'id' && query[key]) {
        macro['parentDataId'] = query[key]
      } else if (key.substr(0, 3) === 'cm_') {
        // cm_开头的key放到宏变量中
        const macroName = key.substr(3)
        macro[macroName] = query[key]
      }
    })
    // 待办已办页面
    if (state.currentTab === 'status0') {
      macro['processStatus'] = '0'
    } else if (state.currentTab === 'status1') {
      macro['processStatus'] = '1'
    }
    return macro
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
